package com.lly.songhigh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lly.songhigh.R;
import com.lly.songhigh.activity.SearchMVDetialsActivity;
import com.lly.songhigh.adapter.MVDetialsListViewAdapter;
import com.lly.songhigh.application.BaseApplication;
import com.lly.songhigh.entity.MVDetials;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.List;

/**
 * Created by 遇博 on 15/10/27.
 */
public class MVFragment extends Fragment implements AdapterView.OnItemClickListener {
    private String MVUrl;
    private ListView single_listView;
    private VolleySingleton volleySingleton;
    private List<MVDetials> singerSingles;
    private List<MVDetials.DataEntity> singer;
    private MVDetialsListViewAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.single_fragment, null);

        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        Bundle bundle = getArguments();
        MVUrl = bundle.getString("MVUrl");
        single_listView = (ListView) view.findViewById(R.id.single_listView);

        single_listView.setOnItemClickListener(this);
        BaseApplication.getInstance().addActivity(getActivity());
        volleySingleton = VolleySingleton.getInstance();
    }
    private void initData() {
        StringRequest request = new StringRequest(MVUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                if (response != null) {
                    Gson gson = new Gson();
                    MVDetials result = gson.fromJson(response, MVDetials.class);
                    if (result != null && result.getData() != null) {
                        singer =  result.getData();
                        adapter = new MVDetialsListViewAdapter(getActivity(), singer);
                        single_listView.setAdapter(adapter);

                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MVDetials.DataEntity dataEntity = (MVDetials.DataEntity) parent.getItemAtPosition(position);
        int urlId = dataEntity.getId();
        Intent intent = new Intent(getActivity(), SearchMVDetialsActivity.class);
        intent.putExtra("_id", urlId);
        getActivity().startActivity(intent);
    }
}
